//
//  NewFeatureViewController.m
//  01-黑马微博
//
//  Created by lanou3g on 15/9/9.
//  Copyright (c) 2015年 heima. All rights reserved.
//

#import "NewFeatureViewController.h"
#define HWNewfeasureCount 4
#import "HWTabBarViewController.h"
@interface NewFeatureViewController ()<UIScrollViewDelegate>
@property (nonatomic, weak) UIPageControl *pageControl;
@end

@implementation NewFeatureViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
  //创建scrollowView
    UIScrollView *scrollowView = [[UIScrollView alloc] init];
    scrollowView.delegate = self;
    scrollowView.frame = self.view.bounds;
    CGFloat scrollW = scrollowView.width;
    CGFloat scrollH = scrollowView.height;
    [self.view addSubview:scrollowView];
    //2.添加图片
    for (int i = 0; i < HWNewfeasureCount; i++) {
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.width = scrollW;
        imageView.height = scrollH;
        imageView.x = i* scrollW;
      NSString *imageName = [NSString stringWithFormat:@"new_feature_%d", i+1];
        imageView.image = [UIImage imageNamed:imageName];
        [scrollowView addSubview:imageView];
        //如果是最后一个imageview，就往里面添加内容
        if (i == HWNewfeasureCount - 1) {
            [self setupLastImageView:imageView];
        }
    }
    scrollowView.contentSize = CGSizeMake(scrollW * HWNewfeasureCount, scrollowView.height);
    scrollowView.bounces = NO;
    scrollowView.pagingEnabled = YES;
    scrollowView.showsHorizontalScrollIndicator = NO;
//    添加pagecontrol
    UIPageControl *pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = HWNewfeasureCount;
//    pageControl.height = 50;
    pageControl.width = 100;
    pageControl.centerX = scrollW * 0.5;
    pageControl.centerY = scrollH - 50;
    pageControl.pageIndicatorTintColor = HWColor(189, 189, 189);
    pageControl.currentPageIndicatorTintColor = HWColor(253, 98, 42);
    [self.view addSubview:pageControl];
    self.pageControl = pageControl;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)setupLastImageView:(UIImageView *)imageView{
//    开启交互
    imageView.userInteractionEnabled = YES;
//    1.分享给大家（checkbox）
    UIButton *shareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_false"] forState:UIControlStateNormal];
    [shareBtn setImage:[UIImage imageNamed:@"new_feature_share_true"] forState:UIControlStateSelected];
    [shareBtn setTitle:@"分享给大家" forState:UIControlStateNormal];
    [shareBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    shareBtn.titleLabel.font = [UIFont systemFontOfSize:17];
    shareBtn.width = 130;
    shareBtn.height = 30;
    shareBtn.centerX = imageView.width * 0.5;
    shareBtn.centerY = imageView.height * 0.7;
    shareBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 20);
    //坚挺地爱
    [shareBtn addTarget:self action:@selector(shareClick:) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:shareBtn];
    
//    2.开始按钮
    UIButton *startBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button"] forState:UIControlStateNormal];
    [startBtn setBackgroundImage:[UIImage imageNamed:@"new_feature_finish_button_highlighted"] forState:UIControlStateHighlighted];
    [startBtn setTitle:@"开始微博" forState:UIControlStateNormal];
    startBtn.width = shareBtn.width;
    startBtn.height = shareBtn.height;
    startBtn.x = shareBtn.x  ;
    startBtn.centerY = imageView.height * 0.78;
    [startBtn addTarget:self action:@selector(startBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [imageView addSubview:startBtn];
}

- (void)startBtnClick{
//    切换窗口的跟控制器
    [UIApplication sharedApplication].keyWindow.rootViewController = [HWTabBarViewController new];
}
- (void)shareClick:(UIButton *)sender{
    sender.selected = !sender.selected;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    double page = scrollView.contentOffset.x / scrollView.width;
//    四舍五入计算页码
    
    self.pageControl.currentPage = (int)(page + 0.5);
}

@end
